package com.banshou.installs.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.ArrayMap;
import android.view.View;

import com.banshou.installs.R;
import com.banshou.installs.constant.Config;
import com.banshou.installs.databinding.FloorSetBinding;
import com.banshou.installs.model.dao.ResponseHisItem;
import com.banshou.installs.model.dao.ResponseEleInfo;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.adapter.EleinfoAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by cjq on 2018/4/22.
 * Email: stoic_yb@139.com
 * features:
 */
public class EleInfoActivity extends BaseActivity {
    private FloorSetBinding mBinding;
    private List<ResponseHisItem> infoList;
    private Map<String, String> map;
    private String eleId;
    private EleinfoAdapter adapter;
    private Map<Integer, ResponseHisItem> infoMap;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.floor_set);
        initView();
    }

    private void initView() {
        infoList = new ArrayList<>();
        infoMap = new TreeMap<>();
        map = new ArrayMap<>();
        eleId = getIntent().getStringExtra(Config.CLICKELEID);
        mBinding.buNext.setVisibility(View.GONE);
        mBinding.title.pagerTitle.setText("安装屏幕信息");
        mBinding.title.back.setOnClickListener(view -> finish());
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new EleinfoAdapter(this, R.layout.ele_info, infoList);
        mBinding.recyclerView.setAdapter(adapter);
        getInfoLis();
    }

    private void getInfoLis() {
        if (eleId != null) {
            map.clear();
            map.put("elevator_id", eleId);
            RetrofitManager2.getInstance()
                    .create(ResponseInfo.class)
                    .loadInfo(map)
                    .compose(RxUtil.lifecycle(this))
                    .subscribe(new Observer<ResponseEleInfo>() {
                        @Override
                        protected void onNextStep(ResponseEleInfo responseEleInfo) {
                            String loucen_info = responseEleInfo.getFloor_info();
                            Map<String, ResponseHisItem> map = new Gson().fromJson(loucen_info, new TypeToken<Map<String, ResponseHisItem>>() {
                            }.getType());
                            if (map != null) {
                                infoList.clear();
                                infoMap.clear();
                                Set<String> strings = map.keySet();
                                for (String string : strings) {
                                    infoMap.put(Integer.valueOf(string), map.get(string));
                                }
                                Set<Integer> integers = infoMap.keySet();
                                for (Integer integer : integers) {
                                    infoList.add(infoMap.get(integer));
                                }
                                adapter.notifyDataSetChanged();
                            }
                        }

                        @Override
                        protected void onErrorResult() {

                        }
                    });
        }

    }
}
